<?php
class ControllerLocalisationGeoZone extends AdminController {

	public function index() {
		M('localisation/geo_zone');
		$this->getList();
	}

	public function insert() {
		M('localisation/geo_zone');

		if ($this->request->isPost() && $this->validateForm()) {
			$this->model_localisation_geo_zone->addGeoZone($this->request->post);

			$this->session->set_flashdata('success', $this->_['text_success']);
			$this->redirect(UA('localisation/geo_zone'));
		}

		$this->getForm();
	}

	public function update() {
		M('localisation/geo_zone');

		if ($this->request->isPost() && $this->validateForm()) {
			$this->model_localisation_geo_zone->editGeoZone($this->request->get['geo_zone_id'], $this->request->post);

			$this->session->set_flashdata('success', $this->_['text_success']);
			$this->redirect(UA('localisation/geo_zone'));
		}

		$this->getForm();
	}

	public function delete() {
		M('localisation/geo_zone');

		if (isset($this->request->post['selected']) && $this->validateDelete()) {
			foreach ($this->request->post['selected'] as $geo_zone_id) {
				$this->model_localisation_geo_zone->deleteGeoZone($geo_zone_id);
			}

			$this->session->set_flashdata('success', $this->_['text_success']);
			$this->redirect(UA('localisation/geo_zone'));
		}

		$this->getList();
	}

	protected function getList() {
		$this->document->setTitle($this->_['heading_title']);

		$qf = new Query_filter();

		$geo_zone_total = $this->model_localisation_geo_zone->getTotalGeoZones();
		$this->data['geo_zones'] = $this->model_localisation_geo_zone->getGeoZones($qf);

		$this->data['pagination'] = $this->pagination($geo_zone_total)->render();

		$this->display('localisation/geo_zone_list.tpl');
	}

	protected function getForm() {
		$this->document->setTitle($this->_['heading_title']);

		if (isset($this->request->get['geo_zone_id'])) {
			$this->data['action'] = UA('localisation/geo_zone/update', 'geo_zone_id=' . $this->request->get['geo_zone_id']);
			if (!$this->request->isPost()) {
				$this->vo->data = $this->model_localisation_geo_zone->getGeoZone($this->request->get['geo_zone_id']);
			}
		} else {
			$this->data['action'] = UA('localisation/geo_zone/insert');
		}

		$this->data['countries'] = cache_read('country.php');

		if (isset($this->request->post['zone_to_geo_zone'])) {
			$this->data['zone_to_geo_zones'] = $this->request->post['zone_to_geo_zone'];
		} elseif (isset($this->request->get['geo_zone_id'])) {
			$this->data['zone_to_geo_zones'] = $this->model_localisation_geo_zone->getZoneToGeoZones($this->request->get['geo_zone_id']);
		} else {
			$this->data['zone_to_geo_zones'] = array();
		}

		$this->display('localisation/geo_zone_form.tpl');
	}

	protected function validateForm() {
		$pass = true;
		if (!range_length($this->request->post['name'], 3, 32)) {
			$this->setMessage('error_name', $this->_['error_name']);
			$pass = false;
		}

		if (!range_length($this->request->post['description'], 3, 255)) {
			$this->setMessage('error_description', $this->_['error_description']);
			$pass = false;
		}

		return $pass;
	}

	protected function validateDelete() {
		M('localisation/tax_rate');

		foreach ($this->request->post['selected'] as $geo_zone_id) {
			$tax_rate_total = $this->model_localisation_tax_rate->getTotalTaxRatesByGeoZoneId($geo_zone_id);

			if ($tax_rate_total) {
				$this->setMessage('error_warning', sprintf($this->_['error_tax_rate'], $tax_rate_total));
				return false;
			}
		}
		return true;
	}
}
?>